package com.zzptc.zzsgz.bdsjws.activities;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.services.IVersionUpdate;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import com.zzptc.zzsgz.bdsjws.vo.VersionInfo;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CheckUpdateActivity extends BaseActivity {


    @BindView(R.id.pic_one)
    ImageView picOne;
    @BindView(R.id.pic_two)
    ImageView picTwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getContentXML() {
        return R.layout.activity_check_update;
    }

    @Override
    public void initData() {
         //检查网络状态 2无网  1 有网络，但比较慢 窗体关闭  3  ，网络正常，可以下载  4、网络正常，已经是最新版本
        boolean b=checkNet();
        if(b){
           //访问服务器使用框架 retrofit  取JSON数据  取到apkUrl  判断版本是否是最新的
            Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl("http://10.0.2.2:8080/")
                   // .baseUrl("http://192.168.1.137:8080/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            IVersionUpdate versionUpdate=retrofit.create(IVersionUpdate.class);//创建接口对象
            Call<VersionInfo> versionInfoCall=versionUpdate.getVersion();
         //   versionInfoCall.execute()
            //异步处理
            versionInfoCall.enqueue(new Callback<VersionInfo>() {
                @Override
                public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                    //网络正常  3  ，网络正常，可以下载  4、网络正常，已经是最新版本
                     VersionInfo versionInfo= response.body();
                    int netcode=versionInfo.getVersioncode();//最新的code
                   int oldcode= MyUtil.getVersionCode(MyApplication.getContext());
                    if(netcode>oldcode){
                       // setResult(3);因为要将VersionInfo 传递父类ACtivity
                        Intent intent=new Intent();
                        intent.putExtra("VersionInfo",versionInfo);
                        setResult(3,intent);
                        finish();
                    }else{
                        setResult(4);
                        finish();
                    }

                }

                @Override
                public void onFailure(Call<VersionInfo> call, Throwable t) {
                    //网络比较慢  关闭窗体
                   setResult(1);
                    finish();

                }
            });


        }else{
            setResult(2);//传值
            finish();
        }
    }

    private boolean checkNet() {
        //网络是怎样的状态
        NetworkInfo.State wifiState = null;
        NetworkInfo.State mobileState = null;
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED == mobileState) {
            // 手机网络连接成功
            Toast.makeText(this, "手机网络连接成功", Toast.LENGTH_SHORT).show();
            return true;
        } else if (wifiState != null && mobileState != null
                && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED != mobileState) {
            // 手机没有任何的网络
            Toast.makeText(this, "手机没有任何的网络", Toast.LENGTH_SHORT).show();
            return false;
        } else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
            // 无线网络连接成功
            Toast.makeText(this, "无线网络连接成功", Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }

    @Override
    public void initView() {
        //动画
        ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(picOne, "scaleY", 1f, 150f, 1f);
        ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(picOne, "scaleX", 1f, 150f, 1f);
        final AnimatorSet set = new AnimatorSet();
        set.play(objectAnimatorX).with(objectAnimatorY);
        set.setDuration(5000);
        set.start();

        ObjectAnimator objectAnimatorY1 = ObjectAnimator.ofFloat(picTwo, "scaleY", 1f, 150f, 1f);
        ObjectAnimator objectAnimatorX1 = ObjectAnimator.ofFloat(picTwo, "scaleX", 1f, 150f, 1f);
        final AnimatorSet set1 = new AnimatorSet();
        set1.play(objectAnimatorX1).with(objectAnimatorY1);
        set1.setDuration(5000);

        //监听
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                set1.start();
            }
        });

        set1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                set.start();
            }
        });


    }
}
